#!/bin/bash
SCRIPT_BASE_DIR=$( dirname "${BASH_SOURCE[0]}" )
source "${SCRIPT_BASE_DIR}/common/fs_utils.sh"

function usage() {
    echo "$( basename $0 ) [-1|-2] <app_dir>"
    echo "    Deploy app in <app_dir> to qemu1 or qemu2"
    echo "    参数说明:"
    echo "        -1: 部署到qemu1, 默认"
    echo "        -2: 部署到qemu2"
    exit 1
}

set -e 

qemu=1

shiftCnt=0
while getopts ":12" args
do
    case "$args" in
        1)
            shiftCnt=$(( $shiftCnt + 1 ))
            qemu=1
            ;;
        2)
            shiftCnt=$(( $shiftCnt + 1 ))
            qemu=2
            ;;
        *)
            usage
            ;;
    esac
done

shift "${shiftCnt}"
appDirs=( "$@" )

# echo ${appDirs[@]}
# echo ${#appDirs[@]}
# exit

if [ 0 -eq ${#appDirs[@]} ]
then
    usage
fi


for appDir in "${appDirs[@]}"
do
    dir_exists_or_die "${appDir}"
    # 复制文件
    echo ara-cli TargetOperation --app "${appDir}"
    ara-cli TargetOperation --app "${appDir}"
    echo "*************************************************************"
    sleep 1

    # 部署
    echo ara-cli DeployAppContainer --app-dir "${appDir}" --target-host "root@fd00::eb:${qemu}"
    ara-cli DeployAppContainer --app-dir "${appDir}" --target-host "root@fd00::eb:${qemu}"

    echo "*************************************************************"
    sleep 1
done
